//
//  BaseNavigationController.m
//  IM
//
//  Created by limaohuyu on 2020/7/3.
//  Copyright © 2020 czw. All rights reserved.
//

#import "BaseNavigationController.h"
#import "AppDelegate.h"

@interface BaseNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation BaseNavigationController

- (void)viewDidLoad{
    [super viewDidLoad];
     self.isSideslip = NO;
    /*
     设置title的颜色
     */
     [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor blackColor]}];
}
- (void)setIsSideslip:(BOOL)isSideslip{
    _isSideslip = isSideslip;
    if (isSideslip) {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
    }else{
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.delegate = self;
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
    
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{

    if (self.childViewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIButton *button = [[UIButton alloc] init];
        [button setImage:[UIImage imageNamed:@"nav_btn_back_black"] forState:UIControlStateNormal];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        button.bounds = CGRectMake(0, 0, 25, 40);
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    //同时满足两个条件1.禁止侧滑。2.实现返回按钮的Block
    if (self.isSideslip && self.navigationBackBlock) {
        self.navigationBackBlock();
        return;
    }
    [self popViewControllerAnimated:YES];
}

#pragma mark  是否允许手势启动
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    if (self.viewControllers.count <= 1) return NO;
    
    return YES;
}


@end
